from django.shortcuts import render
from rest_framework.generics import GenericAPIView

from utils.jwt_auth import create_token
from utils.password_encode import get_md5
from utils import ResponseMessage
from rest_framework.views import APIView
from apps.user.models import User
from apps.user.serializers import UserSerializer
from django.http import HttpResponse, JsonResponse


# Create your views here.
class UserRegisterAPIView(APIView):
    # 注册功能的实现(写法一)
    # def post(self, request):
    #     request.data['password'] = get_md5(request.data['password'])
    #     # 反序列化， 把json变成一个对象 [这是关键]
    #     use_data_serializer = UserSerializer(data=request.data)
    #     use_data_serializer.is_valid(raise_exception=True)
    #     user_data = User.objects.create(**use_data_serializer.data)
    #
    #     # 序列化一下，把json返回给前端对象
    #     user_ser = UserSerializer(instance=user_data)
    #     return JsonResponse(user_ser.data)

    # 注册功能的实现(写法二)
    def post(self, request):
        # print(type(request.data))
        # 反序列化， 把json变成一个对象 [这是关键]
        # print("---------", request.data)
        username = request.data.get("username")
        mobile = request.data.get("mobile")
        password = request.data.get("password")
        request.data["name"] = username
        request.data["mobile"] = mobile
        request.data["password"] = password
        use_data_serializer = UserSerializer(data=request.data)
        use_data_serializer.is_valid(raise_exception=True)
        user_data = use_data_serializer.save()
        # 序列化一下，把json返回给前端对象
        user_ser = UserSerializer(instance=user_data)
        # return JsonResponse(user_ser.data)
        # print(user_ser.data)
        return ResponseMessage.UserResponse.success(user_ser.data)

    def get(self, request):
        mobile = request.GET.get('mobile')
        try:
            user_data = User.objects.get(mobile=mobile)
            user_ser = UserSerializer(user_data)
            return ResponseMessage.UserResponse.success(user_ser.data)
        except Exception as e:
            print(e)
            return ResponseMessage.UserResponse.failed("用户信息获取失败")


# 登录接口
class LoginView(GenericAPIView):
    def post(self, request):
        return_data = {}      # 定义一个字典，便于后面处理数据使用
        request_data = request.data
        mobile = request_data.get('username')
        try:
            user_data = User.objects.get(mobile=mobile)
        except Exception:
            return ResponseMessage.UserResponse.other("用户名或者是密码错误1")
        if not user_data:
            return ResponseMessage.UserResponse.other("用户名或者是密码错误2")
        else:
            user_ser = UserSerializer(instance=user_data, many=False)
            # 用户输入的密码
            user_pwd = request_data.get("password")
            md5_user_pwd = get_md5(user_pwd)
            print(md5_user_pwd)
            # 数据库的密码
            db_user_pwd = user_ser.data.get("password")
            print(db_user_pwd)
            if md5_user_pwd != db_user_pwd:
                return ResponseMessage.UserResponse.other("用户名或者是密码错误3")
            else:
                token_info = {
                    "username": mobile
                }
                token_data = create_token(token_info)
                return_data['token'] = token_data
                return_data['username'] = user_ser.data.get("name")
                return ResponseMessage.UserResponse.success(return_data)


# e10adc3949ba59abbe56e057f20f883e
# e10adc3949ba59abbe56e057f20f883e
